<template>
  <div class="app-container flex-cloumn">
    <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
    >
      <el-form-item label="环境名称" prop="envName">
        <el-input
            v-model="queryParams.envName"
            placeholder="请输入环境名称,回车查询"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="前置URL" prop="address">
        <el-input
            v-model="queryParams.address"
            placeholder="请输入前置URL,回车查询"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="">
        <el-checkbox-group v-model="queryParams.envStatuss" @change="handleQuery">
          <el-checkbox v-for="dict in sys_env_status" :key="dict.dictValue" :label="dict.dictValue"
                       :value="dict.dictValue">
            {{ dict.dictLabel }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
                   v-hasPermi="['testmanage:env:list']"
        >搜索
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-button
          type="primary"
          style="margin: 3px 3px"
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['testmanage:env:insert']"
      >新增
      </el-button
      >

      <el-button
          type="success"
          style="margin: 3px 3px"
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['testmanage:env:update']"
      >修改
      </el-button
      >

      <el-button
          type="danger"
          style="margin: 3px 3px"
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['testmanage:env:delete']"
      >删除
      </el-button
      >
      <!--      <el-button
              type="success"
              style="margin: 3px 3px"
              icon="Unlock"
              :disabled="single || enableFlag"
              @click="handleEnable"
              v-hasPermi="['testmanage:env:update']"
              >启用</el-button
            >

            <el-button
              type="danger"
              style="margin: 3px 3px"
              icon="Lock"
              :disabled="single || stopFlag"
              @click="handleStop"
              v-hasPermi="['testmanage:env:update']"
              >停用</el-button
            >-->

      <el-button
          type="primary"
          style="margin: 3px 3px"
          icon="Plus"
          :disabled="single"
          @click="handleDefault"
          v-hasPermi="['testmanage:env:default']"
      >设为默认
      </el-button
      >

    </el-row>

    <xTable
        ref="configTableRef"
        refId="configTableRef"
        v-loading="loading"
        :data="envList"
        border
        @selection-change="handleSelectionChange"
        class="tableDragger_form"
    >
      <template #columList>
        <el-table-column label="序号" type="index" width="55" align="center"></el-table-column>
        <el-table-column
            label="环境ID"
            align="center"
            prop="envId"
            width="120"
        />
        <el-table-column
            label="环境名称"
            align="left"
            prop="envName"
            :show-overflow-tooltip="true"
        />
        <el-table-column
            label="前置URL"
            align="left"
            prop="address"
            :show-overflow-tooltip="true"
        />

        <el-table-column
            label="状态"
            align="center"
            prop="envStatus" width="150"
            :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <el-switch
                v-model="scope.row.envStatus"
                class="ml-2"
                inline-prompt
                style="&#45;&#45;el-switch-on-color: #13ce66; &#45;&#45;el-switch-off-color: #ff4949"
                active-text="启用"
                inactive-text="停用"
                :active-value="1"
                :inactive-value="0"
                @change="handleStopOrEnable($evnet, scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column v-if="true" label="备注" align="left" prop="remark">

        </el-table-column>

        <el-table-column
            label="操作"
            align="center"
            width="150"
            class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleRowUpdate(scope.row)"
                v-hasPermi="['system:config:edit']"
            >修改
            </el-button
            >
            <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:config:remove']"
            >删除
            </el-button
            >
          </template>
        </el-table-column>
      </template>
    </xTable>

    <pagination
        style="margin: 20px 0 20px 0"
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改环境配置对话框 -->
    <el-dialog
        :title="title"
        v-model="open"
        width="500px"
        :close-on-click-modal="false"
        draggable
        destroy-on-close
        append-to-body
    >
      <el-form ref="envRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="环境名称" prop="envName">
          <el-input v-model="form.envName" placeholder="请输入环境名称"/>
        </el-form-item>
        <el-form-item label="前置URL" prop="address">
          <el-input v-model="form.address" placeholder="请输入前置URL"/>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" :loading="saveLoading">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Env">

import {getCurrentInstance,ref,reactive,toRefs} from "vue";
const {proxy} = getCurrentInstance();


const envList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const envNames = ref([]);
const single = ref(true);
const enableFlag = ref(true);
const stopFlag = ref(true);

const multiple = ref(true);
const total = ref(0);
const title = ref("");
const action = ref("");
const rows = ref([])

const viewPwd = ref(false)

const data = reactive({
  form: {
    envName: '',
    address: '',
    envStatus: '1',
    remark: '',
    isDefault: '0'
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    envName: undefined,
    envStatuss: ['1', '0'],
  },
  rules: {
    envName: [{required: true, message: "环境名称不能为空", trigger: "blur"}],
    address: [{required: true, message: "前置URL不能为空", trigger: "blur"}],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询环境列表 */
function getList() {
  loading.value = true;
  //
  let data = {}
  Object.assign(data, queryParams.value)

}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    envName: '',
    address: '',
    envStatus: '1',
    remark: '',
    isDefault: '0'
  };
  proxy.resetForm("envRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  enableFlag.value = true
  stopFlag.value = true
  ids.value = selection.map(item => item.envId);
  envNames.value = selection.map(item => item.envName);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
  rows.value = selection.map(item => item);
  if (selection.length === 1) {
    if (selection[0].envStatus === '1') {
      enableFlag.value = false
    } else {
      stopFlag.value = false
    }
  }
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  action.value = 'add'
  open.value = true;
  title.value = "添加环境";
  if (rows.value.length === 1) {
    form.value = Object.assign({}, rows.value[0]);
    form.value.envStatus = String(rows.value[0].envStatus);
    form.value.envId = null;
  }
}

/** 修改按钮操作 */
function handleUpdate() {
  reset();
  action.value = 'update'
  form.value = Object.assign({}, rows.value[0]);
  form.value.envStatus = String(rows.value[0].envStatus);
  //
  open.value = true;
  title.value = "修改环境";
}

function handleRowUpdate(row) {
  reset();
  action.value = 'update'
  form.value = Object.assign({}, row);
  form.value.envStatus = String(row.envStatus);
  open.value = true;
  title.value = "修改环境";
}

function handleStopOrEnable(e, rowVal) {
  //
  let data = {}
  data = Object.assign({}, rowVal);

}

function handleEnable() {
  let data = {}
  Object.assign(data, rows.value[0])
  data.envStatus = '0'

}

function handleStop() {
  if (rows.value[0].isDefault === '1') {
    proxy.$modal.msgError("默认环境不允许停用");
    return
  }
  let data = {}
  Object.assign(data, rows.value[0])
  data.envStatus = '1'

}

/** 提交按钮 */
const saveLoading = ref(false);

function submitForm() {
  proxy.$refs["envRef"].validate(valid => {

  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const names = row.envName || envNames.value;
  const envIds = row.envId || ids.value;

}

/**设置环境为默认操作*/
function handleDefault() {
  if (rows.value[0].envStatus === '1') {
    proxy.$modal.msgError("环境停用无法设置为默认");
    return
  }
  const envId = ids.value;

}

/** 导出按钮操作 */
function handleExport() {

}

/** 刷新缓存按钮操作 */
function handleRefreshCache() {

}

getList();
</script>
